﻿#include <easyx.h>
#include <graphics.h>
#include <fstream>
#include <string>
#include "Widget.h"
#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" ) 
using namespace std;
const int wWidth = 1080;
const int wHeight = 640;
Widget widget;//主窗口
string filePath = "#:intro.txt";
LOGFONT* originFontStyle = new LOGFONT;
HWND hwnd;

CPage* pptPages[20];
int loadPPTFromFile(string path) {
    ifstream fi(path);
    if (!fi) {
        cout << "错误：未找到文件 " << path << " ！" << endl;
        return -1;
    }

    int index = 0;
    string data;
    int lineCounter = 0;
    while (!fi.eof()) {
        lineCounter++;
        getline(fi, data);
        cout << "本次读取的数据：" << data << endl;
        if (data == "PPTPAGE:") {
            pptPages[++index] = new CPage;
            continue;
        }
        else
        {
            if (data == "" || (data[0] == '#')) {
                continue;
            }
            else if (data[1] == 'R') {//矩形
                int x, y, w, h, f, ita, und, stri, r, g, b;
                char fontType[20] = { 0 };
                char content[100] = { 0 };
                bool syntaxCorrect1 = sscanf_s(data.c_str(), 
                    "   RECT:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s", 
                    &x, &y, &w, &h, &f, &ita, &und, &stri, &r, &g, &b, fontType, sizeof(fontType));
                lineCounter++;
                getline(fi, data);
                bool syntaxCorrect2 = sscanf_s(data.c_str(),
                    "%s", content, sizeof(content));
                if (!syntaxCorrect1 || !syntaxCorrect2) {
                    cout << "文件读取错误！" << endl;
                    return -1;
                }
                CRect rect(x, y, w, h, f, ita, und, stri, r, g, b, fontType, content);
                pptPages[index]->AddRect(&rect);
            }
            else if (data[1] == 'E') {//椭圆
                int x, y, a, b;
                bool syntaxCorrect = sscanf_s(data.c_str(), "   ELIPSE:%d,%d,%d,%d", &x, &y, &a, &b);
                if (!syntaxCorrect) {
                    cout << "文件读取错误！" << endl;
                    return -1;
                }
                CElipse elipse(x, y, a, b);
                pptPages[index]->AddElipse(&elipse);
            }
            else if (data[1] == 'I') {//图片
                int x, y, w, h;
                char src[200] = { 0 };
                bool syntaxCorrect = sscanf_s(data.c_str(), "   IMAGE:%d,%d,%d,%d,%s", &x, &y, &w, &h, &src, sizeof(src));
                if (!syntaxCorrect) {
                    cout << "文件读取错误！" << endl;
                    return -1;
                }
                CImage image(x, y, w, h, src);
                pptPages[index]->AddImage(&image);
            }
            else {
                cout << "第 " << lineCounter << " 行数据读取错误，已跳过！" << endl;
            }
        }
    }
    fi.close();
    return index;//返回ppt页数
}

void initGraph() {
    hwnd = initgraph(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
    ShowWindow(hwnd, 0);
    SetWindowPos(hwnd, 0, (GetSystemMetrics(SM_CXSCREEN) - wWidth)/2,
        (GetSystemMetrics(SM_CYSCREEN) - wHeight - 48)/2, wWidth, wHeight + 48, 0);
    ShowWindow(hwnd, 1);
}

int main()
{
    int pageCount;//ppt页数
    pageCount = loadPPTFromFile("intro.txt");

    
    if (pageCount != -1) {
        widget.addPPTFrame(new PPTFrame(pptPages[1]));
    }
    else {
        CRect rect(125, 240, 0, 0, 40, 0, 0, 0, 125, 125, 125, "微软雅黑", "当前无PPT页面，请点击‘打开’以加载PPT文件！");
        pptPages[1] = new CPage;
        pptPages[1]->AddRect(&rect);
        pageCount = 1;
        filePath = "#:Empty";
        widget.addPPTFrame(new PPTFrame(pptPages[1]));
    }
    widget.init(wWidth, wHeight, pageCount);

    initGraph();

    widget.paint();
    widget.eventHandle();
}
